package com.yhyvr.api.controller.store;

import com.alibaba.fastjson.JSON;
import com.lz.kit.LogKit;
import com.lz.kit.ehcache.CacheKit;
import com.tencent.protocol.unifiedorder_protocol.jsapi.JsApiPayModel;
import com.yhyvr.api.handle.ApiData;
import com.yhyvr.api.service.store.ApiOrderRefundService;
import com.yhyvr.api.service.store.ApiOrderService;
import com.yhyvr.api.tool.ViewFactory;
import com.yhyvr.constant.CommonConstant;
import com.yhyvr.constant.WxConstant;
import com.yhyvr.pojo.req.api.ApiReqBody;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.req.api.store.order.*;
import com.yhyvr.pojo.resp.CommonResp;
import com.yhyvr.pojo.resp.HttpResult;
import com.yhyvr.pojo.resp.api.ApiRespData;
import com.yhyvr.pojo.resp.api.user.login.LoginResp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * Created by tech6 on 2016/10/11.
 */

@Controller
public class ApiStoreController{
    @Resource
    private ApiOrderService apiOrderService;
    @Resource
    private ApiOrderRefundService apiOrderRefundService;

    @RequestMapping("/api/store/order/info")
    public @ResponseBody
    ModelAndView orderInfo (@ApiData(h5Visit=true,h5VisitToken = false) ApiReqData<ApiOrderInfoReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiOrderService.orderInfo(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }

    /**
     * 订单列表
     * @param apiReqDataApiReqData
     */
    @RequestMapping("/api/store/order/list")
    public @ResponseBody
    ModelAndView  orderList(@ApiData(tokenValidate = false) ApiReqData<ApiOrderListReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiOrderService.orderList(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }

    /**
     * 分销下单
     * @param apiReqDataApiReqData
     * @return
     */
    @RequestMapping("/api/store/order/fenxiao/make")
    public @ResponseBody
    ModelAndView makeErjiFenxiao (@ApiData(h5Visit=true,h5VisitToken = false) ApiReqData<ApiUnifiedorderReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiOrderService.unifiedorder(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }
    /**
     * 分销购物车下单
     * @param apiReqDataApiReqData
     * @return
     */
    @RequestMapping("/api/store/order/fenxiao/make/card")
    public @ResponseBody
    ModelAndView makeErjiFenxiaoByCard (@ApiData(h5Visit=true,h5VisitToken = false) ApiReqData<ApiUnifiedorderByCardReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiOrderService.unifiedorderByCard(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }



    @RequestMapping("/api/store/page/{f}/{h}")
    public String getHtmlPage(@PathVariable String f,@PathVariable String h){

        return "store/"+f+"/"+h;
    }

    @RequestMapping("/api/store/page/{h}")
    public String getHtmlPage2(@PathVariable String h){

        return "store/"+h;
    }


    /**
     * 查询订单是否支付成功
     * @return
     */
    @RequestMapping("/api/store/order/see/ispay")
    public @ResponseBody ModelAndView seeOrderIsPay(@ApiData(h5Visit = true) ApiReqData<ApiOrderInfoReq> reqApiReqBody){
        return ViewFactory.buildApiJsonpView(apiOrderService.isPaySuccess(reqApiReqBody),reqApiReqBody.getCrossDomain());
    }
    /**
     * 获取支付二维码
     * @param apiReqDataApiReqData
     */
    @RequestMapping("/api/store/order/pay/wx/qrcode")
    public @ResponseBody void weiXinPay(@ApiData(tokenValidate = false) ApiReqData<ApiOrderInfoReq> apiReqDataApiReqData){
        apiOrderService.payByQrcodeWx(apiReqDataApiReqData);
    }


    /**
     * 微信JsPAi支付
     * {body:{orderId:"c23c6c8e1bad42bfb6025b5d0c104eb0"}}
     */
    @RequestMapping("/api/store/order/pay/wx/jsapi")
    public String  weiXinJsPay(String orderId){
        return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ WxConstant.appId +"&redirect_uri=https%3A%2F%2F"+ CommonConstant.getInstance().getApiDomain()+"%2Fapi%2Fstore%2Forder%2Fpay%2Fwx%2Fjsapi%2Fcallback?orderId="+orderId+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
    }
    /**
     * 微信JsPAi支付回调
     */
    @RequestMapping("/api/store/order/pay/wx/jsapi/callback")
    public String  weiXinJsPayCallback(String code, String state, String orderId, ModelMap modelMap){
        System.out.println(new Date().getTime()+"第一111111111次加载..............");
        JsApiPayModel jsApiPayModel = apiOrderService.wxJsApi(code,state,orderId);
        CacheKit.put("wxloginTokenEx",orderId,jsApiPayModel);
        modelMap.put("jsApiPayModel",jsApiPayModel);
        return "wxPay/jsApi";

    }

    /**
     * 微信支付回调api
     * @param request
     */
    @RequestMapping("/api/store/order/pay/wx/callback")
    public @ResponseBody
    HttpResult orderWxPayCallBack(HttpServletRequest request){
        apiOrderService.orderWxPayCallBack(request);
        return CommonResp.getSuccess();
    }


    /**
     * 取消订单
     * @param apiReqDataApiReqData
     */
    @RequestMapping(value = {"/api/proxy/store/order/refund","/api/store/order/refund"})
    public @ResponseBody
    ModelAndView refund(@ApiData(tokenValidate = false) ApiReqData<ApiOrderRefundReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiOrderService.refund(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }

    /**
     * 取消退货申请
     * @param apiReqDataApiReqData
     */
    @RequestMapping(value = {"/api/store/order/refund/goods/cancel"})
    public @ResponseBody
    ModelAndView channelRefundGoodds(@ApiData(tokenValidate = false) ApiReqData<ApiOrderChannelRefundReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiOrderService.channelRefundGoods(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }


    /**
     * 退货申请
     * @param apiReqDataApiReqData
     */
    @RequestMapping(value = {"/api/store/order/refund/goods/apply"})
    public @ResponseBody
    ModelAndView refundGoodsApply(@ApiData(tokenValidate = false) ApiReqData<ApiOrderRefundReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiOrderService.refundGoodsApply(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }

    /**
     * 确认收货
     */
    @RequestMapping(value = {"/api/proxy/store/order/take/over"})
    public @ResponseBody
    ModelAndView takeOver(@ApiData(tokenValidate = false) ApiReqData<ApiOrderTakeOverReq> apiOrderTakeOverReqApiReqData){
        return ViewFactory.buildApiJsonpView(apiOrderService.takeOver(apiOrderTakeOverReqApiReqData),apiOrderTakeOverReqApiReqData.getCrossDomain());
    }

    /**
     * 删除订单
     * @param apiReqDataApiReqData
     */
    @RequestMapping("/api/store/order/delete")
    public @ResponseBody
    ModelAndView delete(@ApiData(tokenValidate = false) ApiReqData<ApiOrderInfoReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiOrderService.deleteOrder(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }

    /**
     * 退货信息完善
     * @param apiReqDataApiReqData
     */
    @RequestMapping("/api/store/order/refund/goods/update")
    public @ResponseBody
    ModelAndView refundGoods(@ApiData(tokenValidate = false) ApiReqData<ApiOrderRefundGoodsUpdateReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiOrderRefundService.refundGoodsUpdate(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }

    /**
     * 退货详情
     * @param apiReqDataApiReqData
     */
    @RequestMapping("/api/store/order/refund/goods/detail")
    public @ResponseBody
    ModelAndView refundGoodsDetail(@ApiData(tokenValidate = false) ApiReqData<ApiOrderInfoReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiOrderRefundService.orderRefundInfo(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }

    /**
     * 订单日志
     * @param apiReqDataApiReqData
     */
    @RequestMapping("/api/store/order/log")
    public @ResponseBody
    ModelAndView log(@ApiData(tokenValidate = false) ApiReqData<ApiOrderInfoReq> apiReqDataApiReqData){
        return ViewFactory.buildApiJsonpView(apiOrderService.orderInfoLog(apiReqDataApiReqData),apiReqDataApiReqData.getCrossDomain());
    }

}
